import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))

const SendData = [
	{ name:'站内信', value:'web' },
	{ name:'微信', value:'wx' },
	{ name:'短信', value:'sms' },
	{ name:'邮件', value:'email' },
	{ name:'手机', value:'phone' }
]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 提交表单列表
		formData : [
			{ label: '等级名称',		name: 'level_name', 	full:true, required:true },
			{ label: '等级代码',		name: 'level_code', 	full:true, required:true },
			{ label: '发送方式',		name: 'send_method', 	full:true, required:true,	type: 'checkboxGroup',	 nameStr:'name', idStr:'value', data:SendData,},
			{ label: '排序',			name: 'sort', 			full:true,	type: 'number', },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	// paging
	fetch = () => $fn.fetch.call(this,'conf-msg-level/index')
	
	// 表单
	cols = [
		{ title: '消息级别名称',		dataIndex: 'level_name', 	width:100 },
		{ title: '消息级别代码', 	dataIndex: 'level_code', 	width:150 },
		{ title: '创建时间', 		dataIndex: 'created_at', 	...$fn.table.time },
		{ title: '更新时间', 		dataIndex: 'updated_at', 	...$fn.table.time },
		{ title: '排序', 			dataIndex: 'sort', 			...$fn.table.num },
		{ title: '状态', 			dataIndex: 'enabled', 		...$fn.table.status, render: text => +text === 1 ? '启用' : '禁用' },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text,{uuid, enabled}) => {
		return [
			TableControls.edit.call(this, 'conf-msg-level/info', { uuid }), 						// 编辑
			+enabled === 0 && this.onEnabled(1, uuid),							// 启用
			+enabled === 1 && this.onEnabled(0, uuid),							// 禁用
		]
	}
	// 操作按钮
	controls = () => {
		const { pagingLoading } = this.state
		const btns = [
			{ label:'添加', code:'F2', loading:pagingLoading, onClick:()=> this.modal.open({ formRef:this.formRef }) },
		]
		return btns
	}
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, api:'conf-msg-level/del', param:{ enabled }, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='消息级别' controls={this.controls()}>
					{/* 搜索 */}
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'conf-msg-level/add', editApi:'conf-msg-level/edit' }) }
			</>
		)
	}
}

export default Index